﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerPhanCongGiangDayDao : IPhanCongGiangDayDao
    {
        public SqlServerPhanCongGiangDayDao()
        {
            SQLUtility.TableName = "PhanCongGiangDay";
        }

        //Trả về đối tượng báo ăn
        private PhanCongGiangDay MakePhanCongGiangDay(DataRow row)
        {
            Guid _id = Guid.Parse(row["PhanCongGayID"].ToString());
            Guid _classID = Guid.Parse(row["ClassID"].ToString());
            Guid _schoolYearID = Guid.Parse(row["SchoolYearID"].ToString());
            Guid _nhanVienID = Guid.Parse(row["NhanVienID"].ToString());
            bool _laGVPhuTrach = bool.Parse(row["LaGVPhuTrach"].ToString());

         return new PhanCongGiangDay(_id, _classID, _schoolYearID, _nhanVienID, _laGVPhuTrach);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<PhanCongGiangDay> MakePhanCongGiangDays(DataTable dt)  
        {
            IList<PhanCongGiangDay> list = new List<PhanCongGiangDay>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakePhanCongGiangDay(row));
            }
            return list;
        }
        private DbParameter[] Take(PhanCongGiangDay PhanCongGiangDay)
        {
            DbParameter[] parameters = new DbParameter[5];
            parameters[0] = new SqlParameter("@PhanCongGayID", PhanCongGiangDay.Id);
            parameters[1] = new SqlParameter("@ClassID", PhanCongGiangDay.ClassID);
            parameters[2] = new SqlParameter("@SchoolYearID", PhanCongGiangDay.SchoolYearID);

            parameters[3] = new SqlParameter("@NhanVienID", PhanCongGiangDay.NhanVienID);
            parameters[4] = new SqlParameter("@LaGVPhuTrach", PhanCongGiangDay.LaGVPhuTrach);
            return parameters;
        }
        public IList<Business_Objects.PhanCongGiangDay> GetPhanCongGiangDay()
        {
            SQLUtility.TableName = "PhanCongGiangDay";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakePhanCongGiangDays(dt);
        }

        public Business_Objects.PhanCongGiangDay GetPhanCongGiangDayByID(string PhanCongGiangDayID)
        {
            SQLUtility.TableName = "PhanCongGiangDay";
            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@PhanCongGayID", PhanCongGiangDayID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakePhanCongGiangDay(row);
            }
        }

        public void InsertPhanCongGiangDay(Business_Objects.PhanCongGiangDay PhanCongGiangDay)
        {
            SQLUtility.TableName = "PhanCongGiangDay";
            int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(PhanCongGiangDay), true);
        }

        public int UpdatePhanCongGiangDay(Business_Objects.PhanCongGiangDay PhanCongGiangDay)
        {
            SQLUtility.TableName = "PhanCongGiangDay";
            try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(PhanCongGiangDay)); }
            catch { return -1; }
        }

        public int DeletePhanCongGiangDay(Business_Objects.PhanCongGiangDay PhanCongGiangDay)
        {
            SQLUtility.TableName = "PhanCongGiangDay";
            try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(PhanCongGiangDay)); }
            catch { return -1; }
        }


        public List<PhanCongGiangDay> GetPhanCongGiangDayBySchoolId(string SchoolYearID,int schoolId)
        {
            DbParameter[] parameters = new DbParameter[2];
            parameters[0] = new SqlParameter("@SchoolYearID", SchoolYearID);
            parameters[1] = new SqlParameter("@SchoolID", schoolId);
            DataTable dt = CommandProvider.GetDatatable("usp_SelectPhanCongGiangDaysByAndSchoolYearIDAndSchoolID", parameters);
            return MakePhanCongGiangDays(dt).ToList();  
        }
    }
}
